import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener {
    private JTextField textField;
    private JButton[] buttons;
    private String[] buttonLabels = {
            "C", "+/-", "%", "/",
            "7", "8", "9", "*",
            "4", "5", "6", "-",
            "1", "2", "3", "+",
            "0", ".", "="
    };
    private double num1, num2, result, prevNum;
    private String operator, prevOperator;
    private boolean isResultDisplayed;

    public Calculator() {
        super("Calculator");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(300, 300);
        setLayout(new BorderLayout());
        //创建文本
        textField = new JTextField();
        textField.setEditable(false);
        textField.setPreferredSize(new Dimension(50, 75));
        textField.setFont(new Font("小辉辉", Font.PLAIN, 50));//文本输入框文字大小
        add(textField, BorderLayout.NORTH);
        //创建按钮
        JPanel buttonPane1 = new JPanel(new GridLayout(5, 4));
        buttons = new JButton[buttonLabels.length];
        for (int i = 0; i < buttonLabels.length; i++) {
            buttons[i] = new JButton(buttonLabels[i]);
            //设置按钮字体大小
            buttons[i].setFont(new Font("小辉辉", Font.PLAIN, 24));
            buttons[i].addActionListener(this);
            buttonPane1.add(buttons[i]);
        }
        add(buttonPane1, BorderLayout.CENTER);

        setVisible(true);
    }
    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();
        if (isResultDisplayed && command.matches("[0-9]")) {
            textField.setText("");
            isResultDisplayed = false;
        }
        if (command.equals("C")) {
            num1 = 0;
            num2 = 0;
            operator = null;
            result = 0;
            textField.setText("");
        } else if (command.equals("+/-")) {
            double num = Double.parseDouble(textField.getText());
            textField.setText(Double.toString(-num));
        } else if (command.equals("%")) {
            double num = Double.parseDouble(textField.getText());
            textField.setText(Double.toString(num / 100));
        } else if (command.matches("[0-9]+")) {
            textField.setText(textField.getText() + command);
        } else if (command.equals(".")) {
            if (!textField.getText().contains(".")) {
                textField.setText(textField.getText() + ".");
            }
        } else if (command.matches("[+\\-*/]")) {
            if (result != 0) {
                num1 = result;
                operator = prevOperator;
                prevOperator = command;
                prevNum = Double.parseDouble(textField.getText());
            } else {
                num1 = Double.parseDouble(textField.getText());
                operator = command;
                prevNum = num1;
            }
            textField.setText("");
        } else if (command.equals("=")) {
            if (operator != null) {
                num2 = Double.parseDouble(textField.getText());
                if (operator.equals("+")) {
                    result = num1 + num2;
                } else if (operator.equals("-")) {
                    result = num1 - num2;
                } else if (operator.equals("*")) {
                    result = num1 * num2;
                } else if (operator.equals("/")) {
                    result = num1 / num2;
                }
                prevOperator = operator;
                prevNum = num2;
                textField.setText(Double.toString(result));
                operator = null;
                isResultDisplayed = true;
            }
        }
    }

    public static void main(String[] args) {
        new Calculator();
    }
}